#pragma once
#include <iostream>
#include <string>
#include "Connection.hpp"
#include "comm.hpp"
#include "HandlerConn.hpp"
using namespace std;


class Accepter {
    public :
    static void Accept(Connection* conn) {
        // 获取连接
        struct sockaddr_in peer;
        socklen_t len = sizeof peer;
        while (true) {
            int newsocket = accept(conn->SockFd(), (sockaddr*)&peer, &len);
            if (newsocket > 0) {
                // 将新的文件描述符设置成非阻塞
                SetNonBlock(newsocket);
                Connection* normal_conn = ConnFactory::BuildNormalConnection(newsocket, HandlerConn::recver,\
                HandlerConn::sender, HandlerConn::excepter,EPOLLIN | EPOLLET, conn->_R);
                normal_conn->SetAddr(peer);
                conn->_R->AddConnection(normal_conn);
                lg.LogMessage(Info, "get a new link client info: %s", normal_conn->Get_Info().c_str());
            } else {
                if (errno == EAGAIN) {
                    break;
                } else if (errno == EINTR) {
                    continue;
                } else {
                    lg.LogMessage(Warning, "accept error, errno: %d, error string: %s", errno, strerror(errno));
                    break;
                }
            }
        }

    }
};